/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-08
 * Time: 22:50
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode ql = headA;
        ListNode qs = headB;
        int lenA = 0;
        int lenB = 0;
        while (ql != null) {
            lenA++;
            ql = ql.next;
        }
        while (qs != null) {
            lenB++;
            qs = qs.next;
        }
        ql = headA;
        qs = headB;
        int len = lenA - lenB;
        if (len < 0) {
            ql = headB;
            qs = headA;
            len = lenB - lenA;
        }
        while (len != 0) {
            ql = ql.next;
            len--;
        }
        while (ql != qs) {
            ql = ql.next;
            qs = qs.next;
        }

        return ql;
    }
}
